import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_tcos/flutter_tcos.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final ImagePicker _picker = ImagePicker();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await FlutterTcos.platformVersion ?? 'Unknown platform version';
      final int ret = await FlutterTcos.initCosServer(
          "1300239480",
          "jjgtest",
          "ap-nanjing",
          "http://47.101.214.121:15630/app/thirdpartSdk/getCosCredential",
          "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiYWNjb3VudCI6IiIsInVzZXJfbmFtZSI6IiIsInBob25lIjoiIiwid29rZXIiOiIiLCJtYWlsIjoiIiwic3RhdGUiOjAsImxlYWRlcl9pZCI6MCwiZGVwYXJ0bWVudF9pZCI6MCwicm9sZV9pZCI6MCwibGFzdF9sb2dpbl9pcCI6IiIsImNvbXBhbnlfaWQiOjAsImxhc3RfbG9naW5fdGltZSI6MCwiZGF0YV9wZXJtaXNzaW9ucyI6IiIsInN5c3RlbUlkIjoxMCwiaWF0IjoxNjQ3NzgxMTY5fQ.bElq-tkCX2ovuyo-f3OoXW2RJpWlecngnYA3nwsbOvQ",
          10);
      print(ret);
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            RaisedButton(
          onPressed: () async {
            final XFile? image =
                await _picker.pickImage(source: ImageSource.gallery);
            print(image?.path);
            if (image != null) {
              var ret = await FlutterTcos.upload(image.path, "flutter_test");
              print(ret);
            }
          },
          child: Text(
            "图片上传",
            style: TextStyle(fontSize: 14, color: Colors.blue),
          ),
        ),SizedBox(height: 20,),
        RaisedButton(
          onPressed: () async {
            final XFile? image =
                await _picker.pickImage(source: ImageSource.gallery);
            print(image?.path);
            if (image != null) {
              var ret = await FlutterTcos.uploadWithName(image.path, "flutter_test/a.jpg");
              print(ret);
            }
          },
          child: Text(
            "图片上传2",
            style: TextStyle(fontSize: 14, color: Colors.blue),
          ),
        ),
          ],
        )
      ),
    );
  }
}
